
const columnManager = {
    key:"columnMap",
    /**
     * 批量新增 或同步列对象数据为数据库数据
     * @param sign 列表唯一标识
     * @param columns 列对象列表
     */
    batchSaveOrUpdate(sign, columns) {

        if (sign && columns instanceof Array) {

            const cacheColumnData = this.getColumnCacheBySignName(sign)

            if(cacheColumnData && cacheColumnData instanceof Object){

                this.syncColumnState(columns,cacheColumnData)

                return

            }

            let newColumns = [];

            columns.forEach(column => {
                if(column.field || column.title)
                newColumns.push(this.buildColumn(sign, column))
            })

            this.handleRequest('batchInsertOrGet',newColumns,(res)=>{
                if (res.data && res.data instanceof Object) {
                    const data = res.data
                    this.syncColumnState(columns, data)
                    //TODO 保存缓存
                    this.setColumnCacheBySignName(sign,data)
                }
            },false)
        }
    },syncColumnState(oldColumns,newColumns){
        //将newColumns的数据同步到oldColumns
        oldColumns.forEach(oldColumn=>{
            const newColumn = newColumns[oldColumn.field]
            if(newColumn){
                if(newColumn.visible!=oldColumn.visible)
                    oldColumn.visible = newColumn.visible
            }
        })
    }
    /**
     * 更新列 显隐 状态 同步更新缓存
     * @param sign 列表唯一标识
     * @param column 列对象数据
     */
    ,update(sign,column){
        const updateColumn = this.buildColumn(sign,column)
        axios({
            url:ctx+'system/customColumn',
            method:'put',
            data:updateColumn,
        }).then(res=>{
            if (res.data && res.data instanceof Object) {
                const data = res.data
                //TODO 更新缓存
                const cacheColumnData = this.getColumnCacheBySignName(sign)

                if(cacheColumnData && cacheColumnData instanceof Object){

                    let oldColumn = cacheColumnData[column.field]

                    if(oldColumn){
                        oldColumn.visible = column.visible
                        this.setColumnCacheBySignName(sign,cacheColumnData)
                    }
                }
            }
        })
    },buildColumn(sign,column){
        return {id:column.id?column.id:null,sign:sign,label:column.title,field:column.field,visible:column.visible};
    },handleRequest(methodName,data,callback,async=true){//async默认为 true 异步执行
        let config = {
            url: `${ctx}system/customColumn/${methodName}`,
            type: "post",
            async:async,
            contentType: 'application/json;charset=UTF-8',
            dataType: "json",
            data: JSON.stringify(data),
            success:function(result) {
                if(callback instanceof Function){
                    callback(result)
                }
            },error:function(e){
                console.log(e)
            }
        }
        $.ajax(config)
    },getCacheColumnMap(){
        return storage.local.get(this.key)
    },getColumnCacheBySignName(signName){

        const columnMap = this.getCacheColumnMap()

        if(columnMap && columnMap instanceof Object && columnMap[signName]){
            return columnMap[signName]
        }

        return null;

    },setColumnCacheBySignName(signName,data){

        let columnMap = this.getCacheColumnMap()

        if(!columnMap || !(columnMap instanceof Object)){
            columnMap = {}
        }

        columnMap[signName] = data

        storage.local.set(this.key,columnMap)
    }
}
